/*
 * Toolbar.java
 * 
 * Created by demory on Mar 28, 2009, 5:07:03 PM
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.fpdev.apps.cart.gui;

import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToolBar;
import org.fpdev.apps.cart.CartEvent;
import org.fpdev.apps.cart.Cartographer;
import org.fpdev.core.SysProps;
import org.fpdev.util.gui.ToolbarButton;

/**
 *
 * @author demory
 */
public class Toolbar extends JToolBar implements ActionListener {

  private Cartographer cart_;
  private CartGUI gui_;
  private ButtonGroup btnGroup_;

  private JButton undoBtn_, redoBtn_, zoomInBtn_, zoomOutBtn_;

  public static final int CREATE_ANCHOR_POINT = 101;
  public static final int CREATE_CORRIDOR = 102;
  public static final int DELETE_ANCHOR_POINT = 103;
  public static final int DELETE_CORRIDOR = 104;
  public static final int SPLIT_CORRIDOR = 105;
  public static final int MODIFY_LINE = 106;

  public Toolbar(Cartographer cart, CartGUI gui) {
    super();
    cart_ = cart;
    gui_ = gui;

    btnGroup_ = new ButtonGroup();

    String iconRoot = cart.getProperties().getProperty(SysProps.FP_HOME) + "img" + File.separator;

    undoBtn_ = new JButton(new ImageIcon(iconRoot + "undo.png"));
    redoBtn_ = new JButton(new ImageIcon(iconRoot + "redo.png"));
    addButton(undoBtn_, "Undo");
    addButton(redoBtn_, "Redo");
    undoBtn_.setEnabled(false);
    redoBtn_.setEnabled(false);

    addSeparator();

    zoomInBtn_ = new JButton(new ImageIcon(iconRoot + "zoomin.png"));
    zoomOutBtn_ = new JButton(new ImageIcon(iconRoot + "zoomout.png"));
    addButton(zoomInBtn_, "Zoom In");
    addButton(zoomOutBtn_, "Zoom Out");

    addSeparator();

    newButton("NA", CREATE_ANCHOR_POINT, "New Anchor");
    newButton("NC", CREATE_CORRIDOR, "New Corridor");
    newButton("DA", DELETE_ANCHOR_POINT, "Delete Anchor");
    newButton("DC", DELETE_CORRIDOR, "Delete Corridor");
    newButton("SC", SPLIT_CORRIDOR, "Split Corridor");
    newButton("ML", MODIFY_LINE, "Modify Line");
  }

  public void addButton(JButton btn, String text) {
    btn.addActionListener(this);
    btn.setMargin(new Insets(0, 0, 0, 0));
    btn.setMaximumSize(new Dimension(34, 34));
    btn.setToolTipText(text);
    this.add(btn);
  }

  private ToolbarButton newButton(ImageIcon icon, int action, String tip) {
    return addButton(new ToolbarButton(icon, action), tip);
  }

  private ToolbarButton newButton(String text, int action, String tip) {
    return addButton(new ToolbarButton(text, action), tip);
  }

  private ToolbarButton addButton(ToolbarButton btn, String tip) {
    btn.setToolTipText(tip);
    add(btn);
    btnGroup_.add(btn);
    btn.addActionListener(this);
    return btn;
  }

  public void actionPerformed(ActionEvent e) {
    if(e.getSource() == undoBtn_) 
      cart_.handleEvent(new CartEvent(CartEvent.Type.EDIT_UNDO));
    if(e.getSource() == redoBtn_)
      cart_.handleEvent(new CartEvent(CartEvent.Type.EDIT_REDO));
    if(e.getSource() == zoomInBtn_)
      cart_.handleEvent(new CartEvent(CartEvent.Type.ZOOM_IN));
    if(e.getSource() == zoomOutBtn_)
      cart_.handleEvent(new CartEvent(CartEvent.Type.ZOOM_OUT));
    if(e.getSource() instanceof ToolbarButton) {
      gui_.setToolbarAction(((ToolbarButton) e.getSource()).getClickAction());
    }
  }

  public JButton getUndoButton() {
    return undoBtn_;
  }

  public JButton getRedoButton() {
    return redoBtn_;
  }

}
